package com.hsteach.service.visit;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hsteach.common.config.App;
import com.hsteach.common.config.AppConfig;
import com.hsteach.common.core.domain.model.AuthInfo;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.DateUtils;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.sign.Base64;
import com.hsteach.dao.domain.todothings.Todothings;
import com.hsteach.dao.domain.visit.VisitPeers;
import com.hsteach.dao.domain.visit.VisitRecord;
import com.hsteach.dao.domain.visit.VisitRecordText;
import com.hsteach.dao.mapper.visit.VisitPeersMapper;
import com.hsteach.dao.mapper.visit.VisitRecordMapper;
import com.hsteach.dao.mapper.visit.VisitRecordTextMapper;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.enums.MsgTypeEnum;
import com.hsteach.enums.TimeEnum;
import com.hsteach.enums.TodothingTypeEnum;
import com.hsteach.framework.feign.client.xgk.dto.ret.*;
import com.hsteach.service.third.MessageService;
import com.hsteach.service.third.XgkService;
import com.hsteach.service.todothings.TodothingsService;
import com.hsteach.web.controller.visit.dto.AddReservationDto;
import com.hsteach.web.controller.visit.dto.ReservationDto;
import com.hsteach.web.controller.visit.dto.ReservationListDto;
import com.hsteach.web.controller.visit.vo.ListReservationVo;
import com.hsteach.web.controller.visit.vo.VisitReservationVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.hsteach.dao.mapper.visit.VisitReservationMapper;
import com.hsteach.dao.domain.visit.VisitReservation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 家访预约Service业务层处理
 * 
 * @author fanxquer
 * @date 2023-07-21
 */
@Service
@Slf4j
public class VisitReservationService extends ServiceImpl<VisitReservationMapper,VisitReservation>
{
    @Autowired
    private VisitReservationMapper visitReservationMapper;

    @Autowired
    private VisitRecordMapper visitRecordMapper;

    @Autowired
    private VisitRecordTextMapper visitRecordTextMapper;

    @Autowired
    private XgkService xgkService;

    @Autowired
    private TodothingsService todothingsService;

    @Autowired
    private VisitPeersMapper visitPeersMapper;

    @Autowired
    private MessageService messageService;

    @Autowired
    private AppConfig appConfig;

    /**
     * 查询家访预约
     * 
     * @param id 家访预约主键
     * @return 家访预约
     */
    public VisitReservation selectVisitReservationById(Long id)
    {
        return visitReservationMapper.selectVisitReservationById(id);
    }

    /**
     * 查询家访预约列表
     * 
     * @param visitReservation 家访预约
     * @return 家访预约
     */
    public List<VisitReservation> selectVisitReservationList(VisitReservation visitReservation)
    {
        return visitReservationMapper.selectVisitReservationList(visitReservation);
    }

    /**
     * 查询家访预约列表
     *
     * @return 家访预约
     */
    public List<ListReservationVo> selectLatestReservation(Integer date)
    {
        TimeEnum time = TimeEnum.getByCode(date);
        VisitReservation query = new VisitReservation();
        query.setCreateBy(SecurityUtils.getAuthInfo().getPersonId());
        query.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
        query.setOffset(time.getOffset());
        query.setIsVisited(BooleanEnum.FALSE.getCode());
        List<VisitReservation> visitReservations = visitReservationMapper.selectVisitReservationList(query);
        List<Long> reservationIds = visitReservations.stream().map(VisitReservation::getId).collect(Collectors.toList());
        List<String> personIds = visitReservations.stream().map(VisitReservation::getPersonId).collect(Collectors.toList());
        Map<Long, Long> idMap;
        if (reservationIds.size() > 0) {
            idMap = visitRecordMapper.selectVisitRecordByReservationIds(reservationIds).stream().collect(Collectors.toMap(VisitRecord::getReservationId, VisitRecord::getId, (v1, v2) -> v1));
        } else {
            idMap = Maps.newConcurrentMap();
        }
        Map<String, Integer> countByPersonId;
        if (personIds.size() > 0) {
            countByPersonId = visitRecordMapper.selectCountGroupByPersonIds(personIds).stream().collect(Collectors.toMap(VisitRecord::getPersonId, VisitRecord::getCount, (v1, v2) -> v1));
        } else {
            countByPersonId = Maps.newConcurrentMap();
        }
        List<ListReservationVo> ret = visitReservations.stream().map(e -> {
            // 家长信息
            GetPersonRelationsRet personRelations = xgkService.getPersonRelations(e.getPersonId());
            ListReservationVo vo = new ListReservationVo();
            vo.setGuardianList(personRelations.getGuardianList());
            vo.setReservationId(e.getId());
            vo.setSex(e.getSex());
            vo.setBeginTime(e.getBeginTime());
            vo.setEndTime(e.getEndTime());
            vo.setMobile(e.getParentMobile());
            vo.setPersonName(e.getPersonName());
            vo.setPersonId(e.getPersonId());
            vo.setRecordId(idMap.get(e.getId()));
            Integer orDefault = countByPersonId.getOrDefault(e.getPersonId(), 0);
            vo.setVisitName("第" + (orDefault + 1) + "次家访");
            return vo;
        }).collect(Collectors.toList());
        return ret;
    }

    /**
     * 新增家访预约
     * 
     * @param visitReservation 家访预约
     * @return 结果
     */
    public int insertVisitReservation(VisitReservation visitReservation)
    {
        visitReservation.setCreateTime(DateUtils.getNowDate());
        return visitReservationMapper.insertVisitReservation(visitReservation);
    }

    /**
     * 新增家访预约
     *
     * @param dto 家访预约
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public int addReservation(AddReservationDto dto)
    {
        List<VisitReservation> visitReservations = visitReservationMapper.selectLatestByPersonIds(Lists.newArrayList(dto.getPersonId()));
        if (!CollectionUtils.isEmpty(visitReservations)) {
            throw new ServiceException("该生已被"+ visitReservations.get(0).getCreateByName() +"老师预约，无法进行预约");
        }
        if (dto.getEndTime().getTime() < dto.getBeginTime().getTime()) {
            throw new ServiceException("结束时间不能小于开始时间");
        }
        if (dto.getBeginTime().getTime() < System.currentTimeMillis()) {
            throw new ServiceException("开始时间不能小于当前时间");
        }
        AuthInfo authInfo = SecurityUtils.getAuthInfo();
        VisitReservation visitReservation = new VisitReservation();
        BeanUtils.copyProperties(dto, visitReservation);
        visitReservation.setIsVisited(BooleanEnum.FALSE.getCode());
        visitReservation.setCreateByName(authInfo.getName());
        visitReservation.setCreateBy(authInfo.getPersonId());
        visitReservation.setOrgCode(authInfo.getOrgCode());

        GetPersonRelationsRet personRelations = xgkService.getPersonRelations(dto.getPersonId());
        Guardian guardian = personRelations.getGuardianList().stream().findFirst().orElse(new Guardian());
        visitReservation.setParentMobile(guardian.getMobile());

        List<PersonInfoRet> personInfo = xgkService.getStudentByPersonIds(Lists.newArrayList(dto.getPersonId()));
        if (personInfo.size() > 0) {
            PersonInfoRet personInfoRet = personInfo.get(0);
            visitReservation.setHomeAddr(personInfoRet.getFamilyAddress());
            visitReservation.setSex(personInfoRet.getSex());
        }
        visitReservation.setStatus(0);
        visitReservationMapper.insertVisitReservation(visitReservation);
        // 插入一条家访空记录（草稿箱）
        VisitRecord record = new VisitRecord();
        record.setCreateBy(authInfo.getPersonId());
        record.setCreateByName(authInfo.getName());
        record.setOrgCode(authInfo.getOrgCode());
        record.setPersonId(dto.getPersonId());
        record.setPersonName(dto.getPersonName());
        record.setIsDraft(BooleanEnum.TRUE.getCode());
        record.setDelStatus(BooleanEnum.TRUE.getCode());
        record.setReservationId(visitReservation.getId());
        visitRecordMapper.insertVisitRecord(record);
        VisitRecordText visitRecordText = new VisitRecordText();
        visitRecordText.setRecordId(record.getId());
        String msg =  "【家访预约】"  + SecurityUtils.getAuthInfo().getName()
                + "老师预约您于"+ DateUtils.parseDateToStr("MM月dd日", visitReservation.getBeginTime())+ getGmtTime(visitReservation.getBeginTime())
                +"-"+ getGmtTime(visitReservation.getEndTime())
                +"进行家访，如您该段时间段不便接受家访，请联系老师修改时间。";
        sendToParents(getParentsIds(visitReservation.getPersonId()),msg,String.valueOf(visitReservation.getId()), visitReservation.getPersonId(),false);
        return visitRecordTextMapper.insertVisitRecordText(visitRecordText);
    }

    private String getGmtTime(Date date) {
        TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setTimeZone(timeZone);
        return DateUtils.parseDateToStr("HH:mm", calendar.getTime());
    }

    /**
     * 修改家访预约
     * 
     * @param visitReservation 家访预约
     * @return 结果
     */
    public int updateVisitReservation(VisitReservation visitReservation)
    {
        if (visitReservation.getEndTime().getTime() < visitReservation.getBeginTime().getTime()) {
            throw new ServiceException("结束时间不能小于开始时间");
        }
        if (visitReservation.getBeginTime().getTime() < System.currentTimeMillis()) {
            throw new ServiceException("开始时间不能小于当前时间");
        }
        VisitReservation reservation = visitReservationMapper.selectVisitReservationById(visitReservation.getId());
        if (!reservation.getCreateBy().equals(SecurityUtils.getAuthInfo().getPersonId())) {
            throw new ServiceException("只能操作自己的预约记录");
        }
        visitReservation.setUpdateTime(DateUtils.getNowDate());
        visitReservation.setPassParentIds("");
        //完成代办
//        sendPassMsg(getParentsIds(reservation.getPersonId()),reservation.getPersonId(),reservation.getId());
//        //发起代办
//        String msg =  "【家访时间修改】"  + SecurityUtils.getAuthInfo().getName()
//                + "老师预已修改家访时间为"+ DateUtils.parseDateToStr("MM月dd日", reservation.getBeginTime())+ DateUtils.parseDateToStr("HH:mm", visitReservation.getBeginTime())
//                +"-"+ DateUtils.parseDateToStr("HH:mm", visitReservation.getEndTime())
//                +"进行家访，如您该段时间段不便接受家访，请联系老师修改时间。";
//        sendToParents(getParentsIds(reservation.getPersonId()),msg,String.valueOf(reservation.getId()), reservation.getPersonId(),false);
        passToThing(reservation,visitReservation);
        return visitReservationMapper.updateVisitReservation(visitReservation);
    }

//    @Async
    public void passToThing(VisitReservation reservation,VisitReservation visitReservation){
        //完成代办
        sendPassMsg(getParentsIds(reservation.getPersonId()),reservation.getPersonId(),reservation.getId(),false);
        //发起代办
        String msg =  "【家访时间修改】"  + SecurityUtils.getAuthInfo().getName()
                + "老师预已修改家访时间为"+ DateUtils.parseDateToStr("MM月dd日", reservation.getBeginTime())+ getGmtTime(visitReservation.getBeginTime())
                +"-"+ getGmtTime(visitReservation.getEndTime())
                +"进行家访，如您该段时间段不便接受家访，请联系老师修改时间。";
        sendToParents(getParentsIds(reservation.getPersonId()),msg,String.valueOf(reservation.getId()), reservation.getPersonId(),false);
    }

    /**
     * 批量删除家访预约
     * 
     * @param ids 需要删除的家访预约主键
     * @return 结果
     */
    public int deleteVisitReservationByIds(Long[] ids)
    {
        return visitReservationMapper.deleteVisitReservationByIds(ids);
    }

    /**
     * 删除家访预约信息
     * 
     * @param id 家访预约主键
     * @return 结果
     */
    public int deleteVisitReservationById(Long id)
    {
        VisitReservation reservation = visitReservationMapper.selectVisitReservationById(id);
        if (ObjectUtils.isNotEmpty(reservation) && !reservation.getCreateBy().equals(SecurityUtils.getAuthInfo().getPersonId())) {
            throw new ServiceException("只能操作自己的预约记录");
        }
        VisitReservation update = new VisitReservation();
        update.setId(id);
        update.setDelStatus(BooleanEnum.FALSE.getCode());
        return visitReservationMapper.updateVisitReservation(update);
    }

    /**
     * 家访预约列表
     * @param dto
     * @return
     */
    public List<VisitReservationVO> getList(ReservationListDto dto) {
        if (dto.getPersonId() == null || dto.getType() == null) {
            throw new ServiceException("参数异常");
        }
        List<VisitReservationVO> vos = new ArrayList<>();
        VisitReservation query = new VisitReservation();
        query.setStatus(dto.getType());
        query.setPersonId(dto.getPersonId());
        List<VisitReservation> list = visitReservationMapper.getVisitReservationList(query);
//        List<VisitReservation> list = list(new LambdaQueryWrapper<VisitReservation>()
//                .eq(VisitReservation::getStatus, dto.getType())
//                .eq(VisitReservation::getPersonId,dto.getPersonId()));
        if (CollectionUtils.isEmpty(list)) {
            return vos;
        }
//        List<VisitRecord> records = visitRecordService.list(new LambdaUpdateWrapper<VisitRecord>().in(VisitRecord::getReservationId,list.stream().map(VisitReservation::getId).collect(Collectors.toList())));
        List<VisitRecord> records = visitRecordMapper.selectByReservationIds(list.stream().map(VisitReservation::getId).collect(Collectors.toList()));
        Map<Long, Long> recordsMap = records.stream().collect(Collectors.toMap(VisitRecord::getReservationId, VisitRecord::getId, (v1, v2) -> v1));
//        Map<Long, List<VisitPeers>> peersMap = visitPeersService.list(new LambdaUpdateWrapper<VisitPeers>().in(VisitPeers::getRecordId, records.stream().map(VisitRecord::getId).collect(Collectors.toList())))
//                .stream()
//                .collect(Collectors.groupingBy(VisitPeers::getRecordId));
        Map<Long, List<VisitPeers>> peersMap = visitPeersMapper.selectVisitPeersByRecordIds(records.stream().map(VisitRecord::getId).collect(Collectors.toList()))
                .stream()
                .collect(Collectors.groupingBy(VisitPeers::getRecordId));

//        List<String> personIds = list.stream().map(VisitReservation::getPersonId).collect(Collectors.toList());

//        Map<String, Integer> countByPersonId;
//        if (personIds.size() > 0) {
//            countByPersonId = visitRecordMapper.selectCountGroupByPersonIds(personIds).stream().collect(Collectors.toMap(VisitRecord::getPersonId, VisitRecord::getCount, (v1, v2) -> v1));
//        } else {
//            countByPersonId = Maps.newConcurrentMap();
//        }

        List<PersonInfoRet> personInfo = xgkService.getStudentByPersonIds(Lists.newArrayList(list.get(0).getCreateBy()));
        return list.stream().map(e -> {
                VisitReservationVO vo = new VisitReservationVO();
                vo.setBeginTime(e.getBeginTime());
                vo.setEndTime(e.getEndTime());
                vo.setTeacherPhone(CollectionUtils.isEmpty(personInfo)?"":personInfo.get(0).getMobile());
                List<VisitPeers> peers = CollectionUtils.isEmpty(peersMap.get(recordsMap.get(e.getId())))?new ArrayList<>():peersMap.get(recordsMap.get(e.getId()));
                peers.add(new VisitPeers().setPeerName(e.getCreateByName()));
                vo.setVisitPeers(peers);
                vo.setId(e.getId());
                vo.setReason(e.getReason());
                vo.setIsPass(false);
                if (StringUtils.isNotBlank(e.getPassParentIds())){
                    vo.setIsPass(e.getPassParentIds().contains(SecurityUtils.getAuthInfo().getPersonId()));
                }
//                Integer orDefault = countByPersonId.getOrDefault(e.getPersonId(), 0);
//                vo.setVisitName(e.getPersonName() + "第" + (orDefault + 1) + "次家访");
                return vo;
            }).collect(Collectors.toList());

    }


    /**
     * 取消预约
     * @param dto
     * @return
     */
    public int cancel(ReservationDto dto) {
        VisitReservation reservation = visitReservationMapper.selectVisitReservationById(dto.getId());
        if (ObjectUtils.isNotEmpty(reservation) && !reservation.getCreateBy().equals(SecurityUtils.getAuthInfo().getPersonId())) {
            throw new ServiceException("只能操作自己的预约记录");
        }
        VisitReservation update = new VisitReservation();
        update.setId(dto.getId());
        update.setStatus(2);
        update.setReason(dto.getReason());
        update.setDelStatus(BooleanEnum.FALSE.getCode());
        visitReservationMapper.updateVisitReservation(update);
        sendPassMsg(getParentsIds(reservation.getPersonId()), reservation.getPersonId(), reservation.getId(),true);
        String msg =  "【家访取消】"  + SecurityUtils.getAuthInfo().getName()
                + "老师预约您于"+ DateUtils.parseDateToStr("MM月dd日", reservation.getBeginTime()) + getGmtTime(reservation.getBeginTime())
                +"-"+ getGmtTime(reservation.getEndTime())
                +"的家访已取消，如有疑问，请联系老师。";
        sendToParents(getParentsIds(reservation.getPersonId()),msg,String.valueOf(reservation.getId()),reservation.getPersonId(),true);
        return 1;
    }

//    private void sendMsg(List<String> toPersonIds, String msg,String personId) {
//        List<GetParentsRet> xgkParents = xgkService.getParent(personId);
//        List<String> tIds = xgkParents.stream().flatMap(e -> e.getParents().stream()).map(ParentEntity::getT_personID)
//                .filter(toPersonIds::contains).collect(Collectors.toList());
//        AuthInfo authInfo = SecurityUtils.getAuthInfo();
//        String appId = authInfo.getAppId();
//        App app = appConfig.getConfig().get(appId);
//        if (app == null) {
//            throw new ServiceException("未配置消息跳转地址");
//        }
//        String eStr = "?studentId=" + personId ;
//        String encode = Base64.encode(eStr.getBytes(StandardCharsets.UTF_8));
//        String params = "&e=" + encode ;
//        String url = app.getRedirectUrl() + params;
//        String content = "<p>" + msg + "<a href=\"" + url + "\">跳转进应用</a></p>";
//        messageService.sendMsg(content, tIds, MsgTypeEnum.VISIT_RESERVATION,msg);
//    }

    /**
     * 给家长发待办
     * @param toPersonId
     * @param
     * @param
     */
    public void sendToParents(List<String> toPersonId,String msg, String bId,String personId,Boolean isCancel) {
        List<GetParentsRet> xgkParents = xgkService.getParent(personId);
        List<String> tIds = xgkParents.stream().flatMap(e -> e.getParents().stream()).filter(e -> toPersonId.contains(e.getPersonId()))
                .map(ParentEntity::getT_userID).distinct().collect(Collectors.toList());
        AuthInfo authInfo = SecurityUtils.getAuthInfo();
            String appId = authInfo.getAppId();
            App app = appConfig.getConfig().get(appId);
            if (app == null) {
                throw new ServiceException("未配置消息跳转地址");
            }
            String eStr = "&jfStudentId=" + personId + (isCancel?"&kownid = "+bId:"");
            log.info("代办跳转参数{}",eStr);
            String encode = Base64.encode(eStr.getBytes(StandardCharsets.UTF_8));
//                String encode = URLEncoder.encode(eStr, "UTF-8");
            String params = "&e=" + encode ;
            String url = app.getRedirectUrl() + params;
            log.info("代办跳转地址{}",url);
        String content = "<p>" + msg + "<a href=\"" + url + "\">跳转进应用</a></p>";
        for (String id : tIds) {
            String s = messageService.addTodothings(msg, id, SecurityUtils.getAuthInfo().getOrganizeId(), url, MsgTypeEnum.VISIT_RESERVATION);
            if (s != null) {
                Todothings todothings = new Todothings();
                todothings.setBusinessType(TodothingTypeEnum.ROTA_APPLY.getType());
                todothings.setBusinessKey(bId);
                todothings.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
                todothings.setTodothingsId(s);
                todothings.setToPersonTid(id);
                todothings.setCreateBy(SecurityUtils.getAuthInfo().getPersonId());
                todothingsService.insertTodothings(todothings);
            }
        }
//        messageService.sendMsg(content, tIds, MsgTypeEnum.VISIT_RESERVATION,msg);
    }


    /**
     * 取消代办
     * @param toPersonId
     * @param
     * @param id
     */
    public void sendPassMsg(List<String> toPersonId,String personId,Long id,Boolean isCancel) {
        List<GetParentsRet> xgkParents = xgkService.getParent(personId);
        List<String> tIds = xgkParents.stream().flatMap(e -> e.getParents().stream()).filter(e -> toPersonId.contains(e.getPersonId()))
                .map(ParentEntity::getT_userID).collect(Collectors.toList());

        AuthInfo authInfo = SecurityUtils.getAuthInfo();
            String appId = authInfo.getAppId();
            App app = appConfig.getConfig().get(appId);
            if (app == null) {
                throw new ServiceException("未配置消息跳转地址");
            }
//            String eStr = "/change-shift/details?id=" + id + "&type=apply";
//            String encode = Base64.encode(eStr.getBytes(StandardCharsets.UTF_8));
//            String params = "&e=" + encode ;
//            String url = app.getRedirectUrl() + params;
//            String content = "<p>" + msg + "<a href=\"" + url + "\">跳转进应用</a></p>";
////            messageService.addTodothings(msg, teacherMap.get(toPersonId), SecurityUtils.getAuthInfo().getOrganizeId(), "/test", MsgTypeEnum.TRANSFER_ROTA);
//            messageService.sendMsg(content, Lists.newArrayList(teacherMap.get(toPersonId)), MsgTypeEnum.TRANSFER_ROTA, msg);
            // 处理待办
            Todothings query = new Todothings();
            query.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
            query.setBusinessType(MsgTypeEnum.VISIT_RESERVATION.getTitle());
            query.setBusinessKey(String.valueOf(id));
            query.setToPersonTids(tIds);
            List<Todothings> todothings = todothingsService.getTodothingsList(query);
            if (!CollectionUtils.isEmpty(todothings)) {
                messageService.dealTodothings(todothings.get(0).getTodothingsId());
                return;
            }
//            for (Todothings todothing : todothings) {
//                messageService.dealTodothings(todothing.getTodothingsId());
//            }
    }
    /**
     * 根据学生id获取所有家长信息
     * @return
     */
    public List<String> getParentsIds(String personIds){
        GetPersonRelationsRet personRelations = xgkService.getPersonRelations(personIds);
        return personRelations.getGuardianList().stream().map(Guardian::getGuardianId).collect(Collectors.toList());
    }

    /**
     * 已知晓
     * @return
     */
    public int pass(Long id) {
        String personId = SecurityUtils.getAuthInfo().getPersonId();
        VisitReservation reservation = visitReservationMapper.selectVisitReservationById(id);
        if (ObjectUtils.isEmpty(reservation)) {
            throw new ServiceException("参数异常");
        }
        sendPassMsg(Lists.newArrayList(personId), reservation.getPersonId(), id,false);
        if (StringUtils.isNotBlank(reservation.getPassParentIds()) && reservation.getPassParentIds().contains(personId)) {
            throw new ServiceException("请勿重复点击");
        }
        reservation.setPassParentIds((StringUtils.isNotBlank(reservation.getPassParentIds())?reservation.getPassParentIds()+"," : "")+personId);
        visitReservationMapper.updateVisitReservation(reservation);
        return 1;
    }

}
